package com.gs.SynAntiWandering.adapter;

import android.content.Context;
import android.text.TextUtils;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.adapter.base.BaseAdapter;
import com.gs.SynAntiWandering.adapter.base.BaseViewHolder;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.db.dto.AlertDto;
import com.gs.SynAntiWandering.db.dto.MemberDto;
import com.gs.SynAntiWandering.util.DateUtil;
import com.gs.SynAntiWandering.util.OKHttpUtil;
import com.gs.SynAntiWandering.util.SPUtil;

import java.util.List;


public class AlertAdapter extends BaseAdapter<AlertDto> {


    public AlertAdapter(Context context, List<AlertDto> mDatas, int layoutId) {
        super(context, mDatas, layoutId);
    }

    @Override
    public void convert(BaseViewHolder holder, AlertDto item, final int position) {
        if (item.getDevice() != null) {
            holder.setText(R.id.tv_first, item.getDevice().getName() == null ? "" : item.getDevice().getName());
        }else {
            holder.setText(R.id.tv_first, "");
        }
        holder.setText(R.id.tv_card_no, item.getCardNo() == null ? "" : item.getCardNo());
        int photoType = SPUtil.getInstance().getInt(AllConstants.PHOTO_TYPE, 1);
        if (photoType == 1) {
            setMemberPhoto(holder, item.getMember());
        } else {
            if (TextUtils.isEmpty(item.getPhoto())) {
                setMemberPhoto(holder, item.getMember());
            } else {
                holder.setPersonImageResource(context, R.id.iv_photo, OKHttpUtil.IMG_URL + item.getPhoto());
            }
        }
        if (item.getMember() != null) {
            MemberDto member = item.getMember();
            holder.setText(R.id.tv_fourth, member.getName() == null ? "" : member.getName());
            String floor = "";
            if (member.getFloor() != null) {
                floor = member.getFloor().getName() == null ? "" : member.getFloor().getName();
            }
            String room = member.getRoomNum() == null ? "" : member.getRoomNum();
            String bed = member.getBedNum() == null ? "" : member.getBedNum();
            holder.setText(R.id.tv_fifth, context.getString(R.string.room_info, floor, room, bed));
            if (member.getMemberType() != null) {
                holder.setText(R.id.tv_sixth, member.getMemberType().getName() == null ? "" : member.getMemberType().getName());
            }
        } else {
            holder.setText(R.id.tv_fourth, "");
            holder.setText(R.id.tv_fifth, "");
            holder.setText(R.id.tv_sixth, "");
        }
        if (item.getTime() != null) {
            holder.setText(R.id.tv_second, DateUtil.getInstance().DateTimeToStr(item.getTime()));
            holder.setText(R.id.tv_third, DateUtil.getInstance().DateTimeToStr(item.getTime(), "HH:mm:ss"));
        }else {
            holder.setText(R.id.tv_second,"");
            holder.setText(R.id.tv_third, "");
        }
    }

    private void setMemberPhoto(BaseViewHolder holder, MemberDto member) {
        if (member == null) {
            holder.setImageResource(R.id.iv_photo, R.mipmap.default_person);
        } else {
            holder.setPersonImageResource(context, R.id.iv_photo, OKHttpUtil.IMG_URL + member.getPhoto());
        }
    }
}
